import pygame

# 棋盘的位置数据
POSITION = { 0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9 }
ADJACENT = {
        0: [9, 5],
        1: [5, 6],
        2: [6, 7],
        3: [7, 8],
        4: [8, 9],
        5: [6, 3],
        6: [7, 4],
        7: [8, 0],
        8: [9, 1],
        9: [5, 2],
    }

current_piece = -1

def checkfinish():
    for i in range(10):
        if POSITION[ADJACENT[i][0]] != -1:
            if POSITION[i] == -1 and POSITION[ADJACENT[i][1]] != -1:
                return False
            if POSITION[i] != -1 and POSITION[ADJACENT[i][1]] == -1:
                return False
    if checksuccess():
        print( "count = 1!" )
        if POSITION[0] != -1:
            print( "吉星高照！" )
        if POSITION[6] != -1:
            print( "心想事成！" )
    else:
        print( "count > 1!" )
    return True

def checksuccess():
    count = 0
    for i in range(10):
        if POSITION[i] != -1:
            count += 1
    return count == 1

def check_first():
    for i in range(10):
        if POSITION[i] == -1:
            return False
    return True

def canjump( cur, empty ):
    for i in range(10):
        if i == cur and ADJACENT[i][1] == empty or i == empty and ADJACENT[i][1] == cur:
            if POSITION[ADJACENT[i][0]] != -1: # 中间有棋子
                POSITION[empty] = POSITION[cur]
                POSITION[cur] = -1
                current_piece = empty
                POSITION[ADJACENT[i][0]] = -1 # 删除中间的棋子

def modify_piecesstatus( pos ):
    global current_piece
    if check_first():
        POSITION[pos] = -1
    else:
        if POSITION[pos] != -1: # 选的不是空位，修改当前棋子
            current_piece = pos
        else: # 选的是空位
            if current_piece != -1:  # 检查当前选择棋子选了没
                canjump( current_piece, pos )
    print( "current_piece:", current_piece )
    if checkfinish():
        print( "is finished!" )

def getposiempty( pos ):
    return POSITION[pos] != -1

def getposselected( pos ):
    return current_piece == pos

def calculate_intersection(segment1, segment2):
    # 线段1的起点和终点
    P1, P2 = segment1[0], segment1[1]
    # 线段2的起点和终点
    P3, P4 = segment2[0], segment2[1]

    # 计算斜率
    m1 = (P2[1] - P1[1]) / (P2[0] - P1[0]) if (P2[0] - P1[0]) != 0 else None
    m2 = (P4[1] - P3[1]) / (P4[0] - P3[0]) if (P4[0] - P3[0]) != 0 else None

    # 判断线段关系
    if m1 is None and m2 is None:
        # 两条线段都是垂直线段且平行，无交点
        return None
    elif m1 is None:
        # 线段1是垂直线段，线段2不是
        intersection_x = P1[0]
        intersection_y = m2 * (intersection_x - P3[0]) + P3[1]
        return (intersection_x, intersection_y)
    elif m2 is None:
        # 线段2是垂直线段，线段1不是
        intersection_x = P3[0]
        intersection_y = m1 * (intersection_x - P1[0]) + P1[1]
        return (intersection_x, intersection_y)
    elif m1 == m2:
        # 两条线段平行，无交点
        return None
    else:
        # 计算交点的 x 坐标
        intersection_x = (m1 * P1[0] - m2 * P3[0] - P1[1] + P3[1]) / (m1 - m2)
        # 计算交点的 y 坐标
        intersection_y = m1 * (intersection_x - P1[0]) + P1[1]
        return (intersection_x, intersection_y)

